package com.bjdd.jdy.ui;

import com.baidu.location.BDLocation;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.entity.User;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;

/**
 * Created by yukai on 2016/11/13.
 */

public class MainPresenter extends MainContract.Presenter{
    @Override
    public void onStart() {
        rxManager.on(Constant.EVENT_LOGIN, arg -> view.initUser((User)arg));
        rxManager.on(Constant.EVENT_UPDATE_USER, arg -> view.initUser((User)arg));
        rxManager.on(Constant.EVENT_SET_ALIAS,arg -> setAliasBack((int) arg));
        rxManager.on(Constant.SHOW_LOADING,msg -> showLoadingDialog((Boolean) msg));
        rxManager.on(Constant.HIDE_LOADING,msg -> hideLoadingDialog());
        rxManager.on(Constant.EVENT_LOCATION,msg -> upLoadPosition((BDLocation) msg));
        if (SpUtil.isFirst()){
            JumpUtil.toGuide(context);
            view.finishActivity();
            return;
        }
        if (SpUtil.getUser() == null || SpUtil.getUser().equals("") || SpUtil.getUser().getCarrierId() == null) {
            JumpUtil.toLogin(context);
            view.finishActivity();
            return;
        }
        if (SpUtil.getUser().getExamStatus() != 4 || SpUtil.getUser().getMaterialStatus() != 2){
            JumpUtil.toReady(context);
            view.finishActivity();
            return;
        }
        getUserInfo();
    }
    private void getUserInfo(){
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getCarrierId();
//        showLoadingDialog(false);
        rxManager.add(model.getUserInfo(token,userId)
                .subscribe(result -> {
                    hideLoadingDialog();
                    if (!result.isError()){
                        SpUtil.setUser(result.getData());
                        setAlias(result.getData().getCarrierId());
                        rxManager.post(Constant.EVENT_UPDATE_USER,result.getData());
                    }else {
                        T.show(result.getMsg());
                    }
                },e -> {
                    hideLoadingDialog();
                }));

    }
    private void setAliasBack(int result){
        switch (result){
            case 0:
                L.d("设置别名成功");
                break;
            default:
                L.d("设置别名失败");
                setAlias(SpUtil.getUser().getCarrierId());
                break;
        }
    }
    private void setAlias(String alias){

        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                L.d("开始设置别名："+alias);
                JPushInterface.setAlias(context, alias, new TagAliasCallback() {
                    @Override
                    public void gotResult(int i, String s, Set<String> set) {

                        rxManager.post(Constant.EVENT_SET_ALIAS,i);
                        subscriber.onCompleted();
                    }
                });
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                L.d("线程结束");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {

            }
        });
    }


    @Override
    void upLoadPosition(BDLocation location) {
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        double lng = location.getLongitude();
        double lat = location.getLatitude();
        //T.show("开始上传:"+token);
        rxManager.add(model.upLoadPosition(token,carrierId,lng,lat)
        .subscribe(result -> {
            //T.show("上传定位信息成功");
            L.d("上传定位");
        },e -> {
            //T.show("上传失败："+e.toString());
        }));
    }
}
